package cloud.ipanda;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws IOException {
        System.out.println("Hello World!");
        ConfigureBuilder builder = Configure.builder();
        builder.addPlugin('@',new MyPictureRenderPolicy());//把插件注册为新标签类型

        XWPFTemplate template = XWPFTemplate.compile("d:\\chechCause.docx",builder.build()).render(
                new HashMap<String, Object>(){{
                    put("title", "Hi, poi-tl Word模板引擎");
//                    put("p1", Pictures.ofStream(new FileInputStream("d:\\1.png"), PictureType.PNG)
//                            .size(100, 120).create());
                    BufferedImage bufferedImage= ImageIO.read(new FileInputStream("d:\\1.png"));
                    put("p1", Pictures.ofBufferedImage(bufferedImage, PictureType.PNG)
                            .size(100, 100).create());
                }});
        template.writeAndClose(new FileOutputStream("d:\\new2.docx"));

        wToPdfChange("d:\\new2.docx","d:\\new2.pdf");

    }


    /**
     * word 转 pdf
     *
     * @param wordFile word 的路径 word 的路径
     * @param pdfFile  pdf 的路径
     */
    public static void wToPdfChange(String wordFile, String pdfFile) {
        ActiveXComponent app = null;
        Dispatch document = null;
        System.out.println("========================开始转换========================");
        try {
            // 打开word
            System.out.println("开始打开word");
            app = new ActiveXComponent("Word.Application");

            // 获得word中所有打开的文档
            Dispatch documents = app.getProperty("Documents").toDispatch();
            System.out.println("打开文件: " + wordFile);
            // 打开文档
            document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();
            // 如果文件存在的话，不会覆盖，会直接报错，所以我们需要判断文件是否存在
            File target = new File(pdfFile);
            if (target.exists()) {
                target.delete();
            }
            System.out.println("另存为: " + pdfFile);
            Dispatch.call(document, "SaveAs", pdfFile, 17);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("转换失败" + e.getMessage());
        } finally {
            // 关闭office
//			app.invoke("Quit", 0);

            if (document != null) {
                // 关闭文档
                Dispatch.call(document, "Close", false);
            }
            if (app != null) {
                app.invoke("Quit", 0);
            }

            // 获取系统类型
            String osName = System.getProperty("os.name");
            // 判断是系统类型
            if (osName.toLowerCase().startsWith("win")) {
                System.out.println(osName);
                // window系统
                String killCmd = "taskkill /f /im wps.exe";
                String killCmd1 = "taskkill /f /im wpscenter.exe";
                Process p;
                try {
                    p = Runtime.getRuntime().exec(killCmd);
                    p = Runtime.getRuntime().exec(killCmd1);
                    int runnngStatus = p.waitFor();
                    System.out.println("已杀" + runnngStatus);
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("转换失败" + e.getMessage());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("转换失败" + e.getMessage());
                }
            }
        }
        System.out.println("========================转换结束========================");
    }
}
